package cn.com.headfree.server.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.common.dto.ResultData;

import cn.com.headfree.server.dto.ZuulServerItemDto;
import cn.com.headfree.server.entity.TZuulServerItem;
import cn.com.headfree.server.service.IServerItemService;

@CrossOrigin("*")
@RestController
@RequestMapping(value="/serveritem",produces = "application/json;charset=UTF-8")
public class ServerItemController {
	private static final Logger log = LogManager.getLogger(ServerItemController.class);
	
	@Autowired
	private IServerItemService serverItemService;
	
	/***
	 * @description 保存路由服务信息
	 * @author 冯亚
	 * @date 2020年1月19日 下午4:08:06
	 * @param item
	 * @param request
	 * @return
	 * @return
	 * ResultData
	 */
	@RequestMapping(value = "/save",method=RequestMethod.POST)
	public ResultData save(TZuulServerItem item,HttpServletRequest request) {
		ResultData result = new ResultData();
		try {
			item.setUpdateTime(new Date());
			item.setUpdateIp(request.getRemoteAddr());
			this.serverItemService.save(item);
		} catch (Exception e) {
			log.error("保存路由微服务信息出现了错误！", e);
			result.setSuccess(false);
			result.setErrorMsg("保存路由微服务信息出现了异常！");
		}
		return result;
	}
	
	@RequestMapping(value = "/refresh",method=RequestMethod.GET)
	public ResultData refresh() {
		ResultData result = new ResultData();
		try {
			this.serverItemService.refresh();
		} catch (Exception e) {
			log.error("同步路由微服务信息出现了错误！", e);
			result.setSuccess(false);
			result.setErrorMsg("同步路由微服务信息出现了异常！");
		}
		return result;
	}
	
	/***
	 * @description 更新路由服务信息
	 * @author 冯亚
	 * @date 2020年1月19日 下午4:22:20
	 * @param item
	 * @param request
	 * @return
	 * @return
	 * ResultData
	 */
	@RequestMapping(value = "/update",method=RequestMethod.POST)
	public ResultData update(TZuulServerItem item,HttpServletRequest request) {
		ResultData result = new ResultData();
		try {
			TZuulServerItem dbItem = this.serverItemService.get(TZuulServerItem.class, item.getId());
			if (dbItem != null) {
				dbItem.setIsEnable(item.getIsEnable());
				dbItem.setName(item.getName());
				dbItem.setPath(item.getPath());
				dbItem.setServerId(item.getServerId());
				dbItem.setServiceId(item.getServiceId());
				dbItem.setStripPrefix(item.getStripPrefix());
				dbItem.setUrl(item.getUrl());
				dbItem.setUpdateTime(new Date());
				dbItem.setUpdateIp(request.getRemoteAddr());
				this.serverItemService.update(dbItem);
			} else {
				log.error("更新路由中微服务信息找不到对应的ID记录！");
				result.setSuccess(false);
				result.setErrorMsg("更新路由中微服务信息找不到对应的ID记录！");
			}
		} catch (Exception e) {
			log.error("更新路由微服务信息出现了错误！", e);
			result.setSuccess(false);
			result.setErrorMsg("更新路由微服务信息出现了异常！");
		}
		return result;
	}
	
	/***
	 * @description 删除路由服务信息
	 * @author 冯亚
	 * @date 2020年1月19日 下午4:31:44
	 * @param id
	 * @return
	 * @return
	 * ResultData
	 */
	@RequestMapping(value = "/delete",method=RequestMethod.GET)
	public ResultData delete(@RequestParam(required = true) String id) {
		ResultData result = new ResultData();
		try {
			this.serverItemService.delete(TZuulServerItem.class, id);
		} catch (Exception e) {
			log.error("删除路由微服务信息出现了错误！", e);
			result.setSuccess(false);
			result.setErrorMsg("删除路由微服务信息出现了异常！");
		}
		return result;
	}
	
	@RequestMapping(value = "/getServerItemById",method=RequestMethod.GET)
	public ResultData getServerItemById(String id) {
		ResultData result = new ResultData();
		try {
			TZuulServerItem item = this.serverItemService.get(TZuulServerItem.class, id);
			result.setData(item);
		} catch (Exception e) {
			log.error("查询单记录路由微服务信息出现了错误！", e);
			result.setSuccess(false);
			result.setErrorMsg("查询单记录路由微服务信息出现了异常！");
		}
		return result;
	}
	
	/***
	 * @description 分页查询微服务配置信息
	 * @author 冯亚
	 * @date 2020年1月19日 下午4:55:48
	 * @param serverId
	 * @param isEable
	 * @param page
	 * @return
	 * @return
	 * ResultData
	 */
	@RequestMapping(value = "/find",method=RequestMethod.GET)
	public ResultData findAsPage(String serverId,Integer isEnable,Page<ZuulServerItemDto> page) {
		ResultData result = new ResultData();
		try {
			this.serverItemService.findAsPage(serverId, isEnable, page);
			result.setData(page);
		} catch (Exception e) {
			log.error("查询路由微服务信息出现了错误！", e);
			result.setSuccess(false);
			result.setErrorMsg("查询路由微服务信息出现了异常！");
		}
		return result;
	}
}
